|
PART B - step 2: Wiring the Connections
|
We will now add some clever code to ensure that only one player can be playing at any given moment, thus demonstrating how multiple Beatnik Player instances can effectively be made 'aware' of one another by wiring their event callbacks to a central script. In the enhanced sample below, try the Beatnik control panels again.
| "Ambient" by Brian Salter | |
| "Classical" arranged by Brian Salter | |
| "Jungle" by Brian Salter | |
| "Lounge" by Brian Salter | |
| "Modern Rock" by Paul Sebastien |
It may appear as though some voodoo spell has been cast to change the fundamental behavior of the player itself. Now when you play one song and then start another song playing without stopping the previous song, the song that was playing stops automatically. We even threw in a graceful fadeout so the previous song doesn't stop abruptly, but instead fades out into the new song.
Take a look at the mystery ingredient that has created this behavior. The code that has been added since the previous step is highlighted in red.
<SCRIPT SRC="music-object.js"></SCRIPT>
<SCRIPT LANGUAGE=JavaScript><!-- //
music1 = new Music ();
music2 = new Music ();
music3 = new Music ();
music4 = new Music ();
music5 = new Music ();
lastPlayed = null;
function handleOnPlay (musicObjectID) {
if (lastPlayed != null) {
if (lastPlayed != musicObjectID) {
lastPlayed.stop (true);
}
}
lastPlayed = musicObjectID;
}
music1.onPlay ('handleOnPlay (music1)');
music2.onPlay ('handleOnPlay (music2)');
music3.onPlay ('handleOnPlay (music3)');
music4.onPlay ('handleOnPlay (music4)');
music5.onPlay ('handleOnPlay (music5)');
// --></SCRIPT>
<CENTER>
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=6>
<TR BGCOLOR=EEEEEE>
<TD>
"<B>Ambient</B>" by Brian Salter
</TD>
<TD>
<SCRIPT LANGUAGE=JavaScript><!-- //
music1.magicEmbed (
'SRC="ambient.rmf" ' +
'WIDTH=144 ' +
'HEIGHT=15 ' +
'DISPLAY=SONG ' +
'AUTOSTART=FALSE ' +
'LOOP=FALSE'
);
// --></SCRIPT>
</TD>
</TR>
<TR BGCOLOR=DDDDDD>
<TD>
"<B>Classical</B>" arranged by Brian Salter
</TD>
<TD>
<SCRIPT LANGUAGE=JavaScript><!-- //
music2.magicEmbed (
'SRC="classical.rmf" ' +
'WIDTH=144 ' +
'HEIGHT=15 ' +
'DISPLAY=SONG ' +
'AUTOSTART=FALSE ' +
'LOOP=FALSE'
);
// --></SCRIPT>
</TD>
</TR>
<TR BGCOLOR=CCCCCC>
<TD>
"<B>Jungle</B>" by Brian Salter
</TD>
<TD>
<SCRIPT LANGUAGE=JavaScript><!-- //
music3.magicEmbed (
'SRC="jungle.rmf" ' +
'WIDTH=144 ' +
'HEIGHT=15 ' +
'DISPLAY=SONG ' +
'AUTOSTART=FALSE ' +
'LOOP=FALSE'
);
// --></SCRIPT>
</TD>
</TR>
<TR BGCOLOR=BBBBBB>
<TD>
"<B>Lounge</B>" by Brian Salter
</TD>
<TD>
<SCRIPT LANGUAGE=JavaScript><!-- //
music4.magicEmbed (
'SRC="lounge.rmf" ' +
'WIDTH=144 ' +
'HEIGHT=15 ' +
'DISPLAY=SONG ' +
'AUTOSTART=FALSE ' +
'LOOP=FALSE'
);
// --></SCRIPT>
</TD>
</TR>
<TR BGCOLOR=AAAAAA>
<TD>
"<B>Modern Rock</B>" by Paul Sebastien
</TD>
<TD>
<SCRIPT LANGUAGE=JavaScript><!-- //
music5.magicEmbed (
'SRC="modern-rock.rmf" ' +
'WIDTH=144 ' +
'HEIGHT=15 ' +
'DISPLAY=SONG ' +
'AUTOSTART=FALSE ' +
'LOOP=FALSE'
);
// --></SCRIPT>
</TD>
</TR>
</TABLE>
</CENTER>
<P>
|
In the Cross-connecting a Player and HTML Form Elements example earlier in this tutorial you have seen how functions can be registered as callback handlers for events such as onPause, onPlay, and OnStop.
Well, in addition to being able to take a function as a parameter, the onPause, onPlay, and onStop methods can also take a string as a parameter. This string represents a JavaScript statement (or block of statements) to be executed when the event occurs. In our example, we can now use the same function to handle the callbacks for each of the players, but passing a different parameter to allow the function to discriminate - a code efficiency which simplifies our task tremendously.
See the block of JavaScript code where the onPlay callbacks are registered for the Music Object instances. Each one will end up calling the handleOnPlay function, but with the calling Music Object instance sent as a parameter. For example, music4.onPlay ('handleOnPlay (music4)') will result in the handleOnPlay function being executed every time the Music Object instance called music4 enters into play mode. However, since the statement to be executed is handleOnPlay (music4), when handleOnPlay gets called it will receive music4 as a parameter telling it that the onPlay event was triggered by that instance.
This now leads to the intelligence behind our newly acquired player 'exclusivity' behavior. We can use this central function, handleOnPlay, to maintain a state where only one player can be playing at a time. The way the function does this is to remember the last Music Object instance that was playing and, when a new player enters into play mode, the last playing instance is instructed to stop.
The last playing Music Object is stored in the variable lastPlayed, which is initially set to the value null. The handleOnPlay function performs these two important steps...
There are two additional and more subtle steps that are performed by the function to avoid any quirky behavior.